namespace EgoQrq
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Collections;
	using System.IO;

	/// <summary>
	///		Summary description for ImageViewer.
	/// </summary>
	public abstract class ImageViewer : System.Web.UI.UserControl
	{
		
		protected HtmlTable PicTable = new HtmlTable();
		protected System.Web.UI.WebControls.Image LocalImage;
		protected System.Web.UI.WebControls.HyperLink Previous;
		protected System.Web.UI.WebControls.HyperLink Next;
		protected System.Web.UI.WebControls.Label lblPipe1;
		protected System.Web.UI.WebControls.Label lblPipe2;
		protected System.Web.UI.WebControls.HyperLink ImageIndex;
		private Hashtable config;
		
		private void Page_Load(object sender, System.EventArgs e)
		{
			
			PicTable.Border = 0;
			
			LoadConfigData();

			if (Request["URL"][Request["URL"].Length - 1] == '/')
				DisplayThumbs();
			else
				DisplayImage();
			
			
			//First, set the correct colspan for the blank row at the top:

		}


		private void ThumbsVisible(bool ShowThumbs)
		{
			if (ShowThumbs)
			{
				PicTable.Visible = true;
				LocalImage.Visible = false;
				ImageIndex.Visible = false;
			}
			else
			{
				PicTable.Visible = false;
				LocalImage.Visible = true;
				ImageIndex.Visible = true;
			}

		}

		private void DisplayThumbs()
		{
			//PicTable = new HtmlTable();
			ThumbsVisible(true);
			
			// Add a blank row at the bottom for spacing
			HtmlTableRow row = new HtmlTableRow();
			HtmlTableCell cell = new HtmlTableCell();
			cell.ColSpan = int.Parse(config["columns"].ToString());
			cell.InnerHtml = "&nbsp;";
			row.Cells.Add(cell);
			PicTable.Rows.Add(row);

			row = new HtmlTableRow();

			string[] files = System.IO.Directory.GetFiles(Server.MapPath(Request["URL"]), "TN_*.jpg");
			for (int x = 0; x < files.Length; x++)
			{

				string thumbPath;
				string fullPath;
				
				thumbPath = Request["URL"] + files[x].Substring(files[x].LastIndexOf("\\") + 1);
				
				fullPath = Request["URL"] + files[x].Substring(files[x].LastIndexOf('_') + 1);

				string content = "<a href=\"ImageView.aspx?URL=" + fullPath + "\"><img src=\"" + thumbPath + "\" border=0></a>";
				cell = new HtmlTableCell();
				
				cell.InnerHtml = content;
				cell.Align = "middle";
				row.Cells.Add(cell);

				// Add the row to the rows collection if there are enough cells or we're out of pictures
				if (row.Cells.Count == int.Parse(config["columns"].ToString()) || x == files.Length - 1)
				{
					PicTable.Rows.Add(row);
					row = new HtmlTableRow();
				}

			}

			// Add a blank row at the bottom for spacing
			row = new HtmlTableRow();
			cell = new HtmlTableCell();
			cell.ColSpan = int.Parse(config["columns"].ToString());
			cell.InnerHtml = "&nbsp;";
			row.Cells.Add(cell);
			PicTable.Rows.Add(row);

		}

		private void DisplayImage()
		{
			ThumbsVisible(false);
			LocalImage.ImageUrl = Request["URL"];

			string ImageFileName = Request["URL"].Substring(Request["URL"].LastIndexOf("/") + 1);
			string baseDIR = Request["URL"].Substring(0, Request["URL"].LastIndexOf("/") + 1);

			ImageIndex.NavigateUrl = "ImageView.aspx?URL=" + baseDIR;
			
			string[] files = System.IO.Directory.GetFiles(Server.MapPath(Request["URL"].Substring(0, Request["URL"].LastIndexOf("/"))), "TN_*.jpg");
			for (int x = 0; x < files.Length; x++)
			{
				
				if (GetBaseImage(files[x]) == ImageFileName)
				{
					
					if (x - 1 >= 0)
					{
						Previous.NavigateUrl = "ImageView.aspx?URL=" + baseDIR + GetBaseImage(files[x - 1]);
						Previous.Visible = true;
						lblPipe1.Visible = true;
					}
					
					if (x + 1 < files.Length)
					{
						Next.NavigateUrl = "ImageView.aspx?URL=" + baseDIR + GetBaseImage(files[x + 1]);
						Next.Visible = true;
						lblPipe2.Visible = true;
					}
						
					return;
				}
			}



		}

		private string GetBaseImage(string bImage)
		{
			string CurrentImage = bImage.Substring(bImage.LastIndexOf("\\") + 1);
			return CurrentImage.Substring(3);
		}

		private void LoadConfigData()
		{
			string URL = Request["URL"];

			config = new Hashtable();
			StreamReader s;
			try
			{
				if (System.IO.File.Exists(URL + "config.txt"))
				{
					
					s = new StreamReader(Server.MapPath(URL + "config.txt"));
					string[] sConfig;
					
					while (s.Peek() > -1)
					{
						sConfig = s.ReadLine().Split('=');
						config.Add(sConfig[0].Trim(), sConfig[1].Trim());
					}
					s.Close();
				}
				else
				{
					config.Add("columns", 4);
					config.Add("Name", "Default");
				}
				
			}
			catch(ApplicationException e)
			{
				Response.Write(e.Message);
			}
		}

		

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
